#include "StdAfx.h"
#include "HighLevelShader.h"

namespace rkt
{


	GLenum getGLHighLevelShaderType(ShaderType st)
	{
#if RENDER_SYSTEM == RENDER_SYSTEM_GLES2
		switch(st)
		{
		case ST_VERTEX_PROGRAM:
			return GL_VERTEX_SHADER;
		case ST_FRAGMENT_PROGRAM:
			return GL_FRAGMENT_SHADER;
		}
		return GL_VERTEX_SHADER;
#else
		switch(st)
		{
		case ST_VERTEX_PROGRAM:
			return GL_VERTEX_SHADER_ARB;
		case ST_FRAGMENT_PROGRAM:
			return GL_FRAGMENT_SHADER_ARB;
		}
		return GL_VERTEX_SHADER_ARB;
#endif
	}

	bool HighLevelShader::create(ShaderType st,const uchar *pBuffer,uint len)
	{
		m_type = st;

#if RENDER_SYSTEM == RENDER_SYSTEM_GLES2
		m_handle = glCreateShader(getGLHighLevelShaderType(m_type));
		GLint glLen = len;
		glShaderSource(m_handle, 1, (const char**)&pBuffer, &glLen);
		glCompileShader(m_handle);
		GLint bShaderCompiled;
		glGetShaderiv(m_handle, GL_COMPILE_STATUS, &bShaderCompiled);
		if (!bShaderCompiled)
		{
			int i32InfoLogLength, i32CharsWritten;
			glGetShaderiv(m_handle, GL_INFO_LOG_LENGTH, &i32InfoLogLength);
			char* pszInfoLog = new char[i32InfoLogLength];
			glGetShaderInfoLog(m_handle, i32InfoLogLength, &i32CharsWritten, pszInfoLog);
			//MessageBox(hWnd, i32InfoLogLength ? pszInfoLog : _T(""), _T("Failed to compile vertex shader"), MB_OK|MB_ICONEXCLAMATION);
			printf("Failed to compile fragment shader! : %s\n",pszInfoLog);
			delete[] pszInfoLog;
			glDeleteShader(m_handle);
			return false;
		}
		return true;
#else
		m_handle = glCreateShaderObjectARB(getGLHighLevelShaderType(st));
		if(m_handle)
		{
			glShaderSourceARB(m_handle,1,(const GLcharARB**)&pBuffer,NULL);
			glCompileShaderARB(m_handle);
		}
		return m_handle != 0;
#endif
	}

	bool HighLevelShader::compile()
	{
		return true;
	}

	bool HighLevelShader::bind()
	{
		return true;
	}

	void HighLevelShader::unbind()
	{
	}

	void HighLevelShader::setParameter4f(const std::string& strPara,float,float,float,float)
	{
	}

	void HighLevelShader::setParameter4f(uint index,float x,float y,float z,float w)
	{
	}

	ShaderType HighLevelShader::getType()
	{
		return m_type;
	}

	GLhandleARB HighLevelShader::getHandle()
	{
		return m_handle;
	}
}